package com.baizhi.controller;

import com.baizhi.entity.Book;
import com.baizhi.repository.BookRepository;
import com.baizhi.repository.CustomBookRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

@RestController
@RequestMapping("book")
public class BookController {
    @Autowired
    private BookRepository bookRepository;

    @Autowired
    private CustomBookRepository customBookRepository;//高亮查询所需注入的属性

    @RequestMapping("select2")
    public Map selectForHighlight(String name,Integer page,Integer limit){
        Map map = new HashMap();
        if(name!=null){
            map = customBookRepository.selectByNameForHighlight(page, limit, name);
        }else {
            Page<Book> pageInfo =
                    bookRepository.findAll(PageRequest.of(page - 1, limit, Sort.by(Sort.Order.asc("id"))));
            map.put("count",pageInfo.getTotalElements());//查询出来的数据总条数
            map.put("data",pageInfo.getContent());//查询出来的数据
        }
        map.put("code",0);
        map.put("msg","");
        return map;
    }


    @RequestMapping("add")
    public Map addBook(Book book){
        Map map = new HashMap();
        try {
            bookRepository.save(book);//添加一个数据到es中
            map.put("flag",true);
        } catch (Exception e) {
            map.put("flag",false);
        }
        return map;
    }
    @RequestMapping("remove")
    public Map removeBook(Long id){
        Map map = new HashMap();
        try {
            bookRepository.deleteById(id);//根据ID删除一个
            map.put("flag",true);
        } catch (Exception e) {
            map.put("flag",false);
        }
        return map;
    }
    @RequestMapping("update")
    public Map updateBook(Book book){
        System.out.println(book);
        Map map = new HashMap();
        try {
            bookRepository.save(book);//更新  与添加的方法一样 但es中若有了该数据则执行修改操作
            map.put("flag",true);
        } catch (Exception e) {
            map.put("flag",false);
        }
        return map;
    }
    @RequestMapping("getOne")
    public Book getOneBook(Long id){
        Optional<Book> optionalBook = bookRepository.findById(id);//查询一个
        return optionalBook.get();
    }
    @RequestMapping("select")
    public Map selectBook(String name,Integer page,Integer limit){
        Map map = new HashMap();
        map.put("code",0);
        map.put("msg","");
        if(name!=null && name.trim().length()>0){
            List<Book> list =
                    bookRepository.findByName(name, PageRequest.of(page - 1, limit, Sort.by(Sort.Order.asc("id"))));
            List<Book> list1 = bookRepository.findByName(name);
            map.put("count",list1.size());
            map.put("data",list);
        }else {
            Page<Book> pageInfo =
                    bookRepository.findAll(PageRequest.of(page - 1, limit, Sort.by(Sort.Order.asc("id"))));
            map.put("count",pageInfo.getTotalElements());//查询出来的数据总条数
            map.put("data",pageInfo.getContent());//查询出来的数据
        }
        return map;
    }
}
